package com.xfan.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class StringUtil {
	public static void main(String[] args) {
		String str=StringUtil.join(StringUtil.match("tm0080", "\\d{2}"),":");
		System.out.println(str);
	}
	
	public static String[] match(String str,String rgExp){
		List<String> finds=new ArrayList<String>();
		try {
			Pattern regex = Pattern.compile(rgExp);
			Matcher regexMatcher = regex.matcher(str);
			while (regexMatcher.find()) {
				try {
					finds.add(regexMatcher.group());
				} catch (IllegalStateException ex) {
				} catch (IllegalArgumentException ex) {
				} catch (IndexOutOfBoundsException ex) {
				} 
			}
		} catch (PatternSyntaxException ex) {
		}
		String[] result=new String[finds.size()];
		return finds.toArray(result);
	}
	
	public static String join(String[] target,String separator,boolean ignore){
		StringBuffer sb=new StringBuffer();
		boolean first=true;
		for (int i = 0; i < target.length; i++) {
			if(ignore&&isBlank(target[i])) continue;
			if(!first)sb.append(separator);
			sb.append(target[i]);
		}
		return sb.toString();
	}
	
	public static String join(String[] target,String separator){
		return join(target,separator,true);
	}
	
	public static String[] getArray(List<String> list){
		String[] result=new String[list.size()];
		return list.toArray(result);
	}
	
	public static boolean isEmpty(String str){
		return str==null||"".equals(str);
	}
	
	public static boolean isBlank(String str){
		return str==null||"".equals(str.trim());
	}
}
